package com.ztjy.purchase.biz.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztjy.bean.CProduct;
import com.ztjy.purchase.biz.CProductBiz;
import com.ztjy.purchase.dao.CProductDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CProductBizImpl implements CProductBiz {
    @Autowired
    private CProductDao pdao;

    @Override
    public List<CProduct> findAll(CProduct product) {
        return pdao.findAll(product);
    }

    @Override
    public List<CProduct> findAllByPname(CProduct cProduct) {
        cProduct.setProductStatus(1);
        return pdao.findAllByPname(cProduct);
    }

    @Override
    public CProduct findByIdProduct(Integer id) {
        return pdao.findByIdProduct(id);
    }

    @Override
    public PageInfo<CProduct> findPageProduct(CProduct cProduct, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<CProduct> all = pdao.findAll(cProduct);
        PageInfo<CProduct> cProductPageInfo = new PageInfo<CProduct>(all);
        return cProductPageInfo;
    }

    @Override
    public boolean updateProduct(CProduct cProduct) {
        boolean flag = false;
        try {
            int row = pdao.updateByPrimaryKeySelective(cProduct);
            flag = (row != 0) ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace();
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean addProduct(CProduct cProduct) {
        boolean flag = false;
        try {
            int row = pdao.insertSelective(cProduct);
            flag = (row != 0) ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace();
            flag = false;
        }
        return flag;
    }

    @Override
    public List<CProduct> findbyTypeId(Integer id) {
        return pdao.findbytypeid(id);
    }
}
